package org.dromara.datacheck.controller.jst;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.datacheck.domain.bo.JstAndJDTableDataEntryBo;
import org.dromara.datacheck.domain.bo.jst.JstOtherOutEnterWarehouseOrderBo;
import org.dromara.datacheck.domain.jst.JstOtherOutEnterWarehouseOrder;
import org.dromara.datacheck.domain.vo.jst.JstOtherOutEnterWarehouseOrderImportVo;
import org.dromara.datacheck.domain.vo.jst.JstOtherOutEnterWarehouseOrderVo;
import org.dromara.datacheck.listener.jst.JstOtherOutEnterWarehouseOrderImportListener;
import org.dromara.datacheck.service.jst.IJstOtherOutEnterWarehouseOrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * 聚水潭-其他出入库单
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataCheck/jst/otherOutEnterWarehouseOrder")
public class JstOtherOutEnterWarehouseOrderController extends BaseController {

    private final IJstOtherOutEnterWarehouseOrderService jstOtherOutEnterWarehouseOrderService;

    /**
     * 查询聚水潭-其他出入库单列表
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> list(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstOtherOutEnterWarehouseOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聚水潭-其他出入库单列表
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:export")
    @Log(title = "聚水潭-其他出入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JstOtherOutEnterWarehouseOrderBo bo, HttpServletResponse response) {
        List<JstOtherOutEnterWarehouseOrderVo> list = jstOtherOutEnterWarehouseOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "聚水潭-其他出入库单", JstOtherOutEnterWarehouseOrderVo.class, response);
    }

    /**
     * 获取聚水潭-其他出入库单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:query")
    @GetMapping("/{id}")
    public R<JstOtherOutEnterWarehouseOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jstOtherOutEnterWarehouseOrderService.queryById(id));
    }

    /**
     * 新增聚水潭-其他出入库单
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:add")
    @Log(title = "聚水潭-其他出入库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JstOtherOutEnterWarehouseOrderBo bo) {
        return toAjax(jstOtherOutEnterWarehouseOrderService.insertByBo(bo));
    }

    /**
     * 修改聚水潭-其他出入库单
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:edit")
    @Log(title = "聚水潭-其他出入库单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JstOtherOutEnterWarehouseOrderBo bo) {
        return toAjax(jstOtherOutEnterWarehouseOrderService.updateByBo(bo));
    }

    /**
     * 删除聚水潭-其他出入库单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:remove")
    @Log(title = "聚水潭-其他出入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jstOtherOutEnterWarehouseOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "聚水潭-其他出入库单导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 每次导入之前清空表里的数据
        List<JstOtherOutEnterWarehouseOrder> jstOtherOutEnterWarhouseOrderList = jstOtherOutEnterWarehouseOrderService.queryAll();
        if(CollUtil.isNotEmpty(jstOtherOutEnterWarhouseOrderList)){
            jstOtherOutEnterWarehouseOrderService.deleteAll();
        }
        // 解析Excel文件并处理导入逻辑
        ExcelResult<JstOtherOutEnterWarehouseOrderImportVo> result = ExcelUtil.importExcel(file.getInputStream(), JstOtherOutEnterWarehouseOrderImportVo.class, new JstOtherOutEnterWarehouseOrderImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询聚水潭-其他出入库单列表
     * 场景分类：其他出库-报废，盘亏
     * 业务查询条件：根据out_warehouse_type出库类型为报废，盘亏查询
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list-by-out-warehouse-type1")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> listByOutWareHouseType1(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        bo.setOutWarehouseTypeList(Arrays.asList("报废","盘亏"));
        return jstOtherOutEnterWarehouseOrderService.queryPageListByOutWareHouseType1(bo, pageQuery);
    }

    /**
     * 查询聚水潭-其他出入库单列表
     * 场景分类：其他出库-供应商赔付，盘盈，领用退回
     * 业务查询条件：根据out_warehouse_type出库类型为供应商赔付，盘盈，领用退回查询
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list-by-out-warehouse-type2")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> listByOutWareHouseType2(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        bo.setOutWarehouseTypeList(Arrays.asList("供应商赔付","盘盈","领用退回"));
        return jstOtherOutEnterWarehouseOrderService.queryPageListByOutWareHouseType2(bo, pageQuery);
    }

    /**
     * 查询聚水潭-其他出入库单列表
     * 场景分类：调拨-销退仓,次品仓调拨委外加工仓
     * 业务查询条件：根据out_warehouse_type出库类型为仓库编码+仓库的单子查询
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list-by-out-warehouse-type3")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> listByOutWareHouseType3(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstOtherOutEnterWarehouseOrderService.queryPageListByOutWareHouseType3(bo, pageQuery);
    }

    /**
     * 查询聚水潭-其他出库单列表以及金蝶-其他出库单数据汇总（按日期汇总）
     * 场景分类：6、其他出库-报废，盘亏
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/jst-and-jd-sum-by-date-by-scrap-inventory")
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDateByScrapInventory() throws ParseException {
        return jstOtherOutEnterWarehouseOrderService.jstAndJdSumByDateByScrapInventory();
    }

    /**
     * 查询聚水潭-其他出库单列表以及金蝶-其他入库单数据汇总（按日期汇总）
     * 场景分类：7、其他入库-供应商赔付，盘盈，领用退回
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/jst-and-jd-sum-by-date-by-claim-order-profit-return")
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDateByClaimOrderProfitReturn() throws ParseException {
        return jstOtherOutEnterWarehouseOrderService.jstAndJdSumByDateByClaimOrderProfitReturn();
    }

    /**
     * 查询聚水潭-其他出库单列表以及金蝶-直接调拨单数据汇总（按日期汇总）
     * 场景分类：8、调拨-销退仓,次品仓调拨委外加工仓
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/jst-and-jd-sum-by-date-by-sale-return-transfer")
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDateBySaleReturnTransfer() throws ParseException {
        return jstOtherOutEnterWarehouseOrderService.jstAndJdSumByDateBySaleReturnTransfer();
    }

    /**
     * 查询聚水潭-其他出入库单列表（金蝶缺少的数据）
     * 场景分类：6、其他出库-报废，盘亏
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list-by-date-by-scrap-inventory-by-jd-deficiency")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> listByDateByScrapInventoryByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstOtherOutEnterWarehouseOrderService.queryPageListByScrapInventoryByJdDeficiency(bo, pageQuery);
    }

    /**
     * 导出聚水潭-其他出入库单列表（导出6、其他出库-报废，盘亏的差异数据）
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:export")
    @Log(title = "聚水潭-其他出入库单（导出6、其他出库-报废，盘亏的差异数据）", businessType = BusinessType.EXPORT)
    @PostMapping("/export-scrap-inventory-by-jd-deficiency")
    public void exportScrapInventoryByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,HttpServletResponse response) {
        List<JstOtherOutEnterWarehouseOrderVo> list = jstOtherOutEnterWarehouseOrderService.queryListScrapInventoryByJdDeficiency(bo);
        ExcelUtil.exportExcel(list, "聚水潭-其他出入库单", JstOtherOutEnterWarehouseOrderVo.class, response);
    }

    /**
     * 查询聚水潭-其他出入库单列表（金蝶缺少的数据）
     * 场景分类：7、其他入库-供应商赔付，盘盈，领用退回
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list-by-date-by-claim-order-profit-return-by-jd-deficiency")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> listByDateByClaimOrderProfitReturnByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstOtherOutEnterWarehouseOrderService.queryPageListByClaimOrderProfitReturnByJdDeficiency(bo, pageQuery);
    }

    /**
     * 导出聚水潭-其他出入库单列表（导出7、其他入库-供应商赔付，盘盈，领用退回的差异数据）
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:export")
    @Log(title = "聚水潭-其他出入库单（导出7、其他入库-供应商赔付，盘盈，领用退回的差异数据）", businessType = BusinessType.EXPORT)
    @PostMapping("/export-claim-order-profit-return-by-jd-deficiency")
    public void exportClaimOrderProfitReturnByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,HttpServletResponse response) {
        List<JstOtherOutEnterWarehouseOrderVo> list = jstOtherOutEnterWarehouseOrderService.queryListClaimOrderProfitReturnByJdDeficiency(bo);
        ExcelUtil.exportExcel(list, "聚水潭-其他出入库单", JstOtherOutEnterWarehouseOrderVo.class, response);
    }

    /**
     * 查询聚水潭-其他出入库单列表（金蝶缺少的数据）
     * 场景分类：8、调拨-销退仓,次品仓调拨委外加工仓
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:list")
    @GetMapping("/list-by-date-by-sale-return-transfer-by-jd-deficiency")
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> listByDateBySaleReturnTransferByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstOtherOutEnterWarehouseOrderService.queryPageListBySaleReturnTransferByJdDeficiency(bo, pageQuery);
    }

    /**
     * 导出聚水潭-其他出入库单列表（导出8、调拨-销退仓,次品仓调拨委外加工仓的差异数据）
     */
    @SaCheckPermission("dataCheck:otherOutEnterWarehouseOrder:export")
    @Log(title = "聚水潭-其他出入库单（导出8、调拨-销退仓,次品仓调拨委外加工仓的差异数据）", businessType = BusinessType.EXPORT)
    @PostMapping("/export-claim-order-sale-return-transfer-by-jd-deficiency")
    public void exportSaleReturnTransferByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,HttpServletResponse response) {
        List<JstOtherOutEnterWarehouseOrderVo> list = jstOtherOutEnterWarehouseOrderService.queryListSaleReturnTransferByJdDeficiency(bo);
        ExcelUtil.exportExcel(list, "聚水潭-其他出入库单", JstOtherOutEnterWarehouseOrderVo.class, response);
    }


}
